/*******************************************************************************
 * Copyright (c) 2010-2011 Robert Fraser                                       *
 *                                                                             *
 * Permission is hereby granted, free of charge, to any person obtaining a     *
 * copy of this software and associated documentation files (the "Software"),  *
 * to deal in the Software without restriction, including without limitation   *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
 * and/or sell copies of the Software, and to permit persons to whom the       *
 * Software is furnished to do so, subject to the following conditions:        *
 *                                                                             *
 *   - The above copyright notice and this permission notice shall be included *
 *     in all copies or substantial portions of the Software.                  *
 *                                                                             *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
 * DEALINGS IN THE SOFTWARE.                                                   *
 *******************************************************************************/

using System;
using System.Windows.Controls;
using zeal.core;
using zeal.ui.util;

namespace zeal.ui.graph
{
    internal sealed partial class GraphEdge : BezierPath
    {
        private readonly GraphPort _input;
        private readonly GraphPort _output;

        public GraphPort input { get { return _input; } }
        public GraphPort output { get { return _output; } }

        public GraphEdge(GraphPort input, GraphPort output)
        {
            if(input == null) throw new ArgumentNullException("input");
            if(output == null) throw new ArgumentNullException("output");
            if(!input.isInput) throw new ArgumentException("input is an output", "input");
            if(output.isInput) throw new ArgumentException("output is an input", "output");
            _input = input;
            _output = output;

            // Bind the path's source and destination points to the source and destination's location
            Bindings.set(this, DestinationProperty, input, GraphPort.AnchorPointProperty);
            Bindings.set(this, SourceProperty, output, GraphPort.AnchorPointProperty);

            // This edge is "active" if either the source or destination node is selected. An active
            // edge is placed on top of any unselected nodes 
            Bindings.set(this, IsActiveProperty, 
                Bindings.anyOf(
                    Bindings.get(input.node, GraphNode.IsSelectedProperty),
                    Bindings.get(output.node, GraphNode.IsSelectedProperty)));

            // Place this edge on top of any nodes that get in the way if it's active
            Bindings.set(this, Panel.ZIndexProperty.of<int>(), this, IsActiveProperty, 
                v => v ? GraphEditor.Z_INDEX_SELECTED_EDGES : GraphEditor.Z_INDEX_EDGES);

            // Finally, set up the color binding
            Bindings.set(this, PenProperty, 
                Bindings.multi(
                        Bindings.get<TextureType>(input.port, "type"), // type
                        Bindings.get(this, IsActiveProperty),          // isSelected
                        Bindings.constant(false),                      // isDeletionTarget
                        EdgeColorizer.instance.edgeConverter));
        }
    }
}